package com.gpnu.crm.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpnu.crm.domain.Customer;
import com.gpnu.crm.domain.Employee;
import com.gpnu.crm.mapper.CustomerMapper;
import com.gpnu.crm.query.QueryObject;
import com.gpnu.crm.service.ICustomerService;
import com.gpnu.crm.util.UserContext;

//员工对象在业务层实现，需要交给Spring来创建对象
@Service
public class CustomerServiceImpl implements ICustomerService {

	@Autowired
	private CustomerMapper customerMapper;

	public CustomerServiceImpl() {
	}

	public PageInfo<Customer> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderby());
		List<Customer> list = customerMapper.selectForList(qo);
		return new PageInfo(list);
	}

	public void saveOrUpdate(Customer entity) {
		if (entity.getId() == null) {
			entity.setSeller(UserContext.getCurrentUser());
			entity.setInputUser(UserContext.getCurrentUser());
			entity.setInputTime(new Date());
			entity.setStatus(Customer.STATUS_NORMAL);
			customerMapper.insert(entity);
		} else {
			customerMapper.updateByPrimaryKey(entity);
		}
	}

	public List<Customer> list() {
		return customerMapper.selectAll();
	}

	public void updateStatus(Customer entity) {
		customerMapper.updateCustomerInfo(entity.getId(), null, entity.getStatus());
	}

}
